//
//  AMMainViewController.swift
//  微博
//
//  Created by zhc on 15/11/23.
//  Copyright © 2015年 zhc. All rights reserved.
//

import UIKit

class AMMainViewController: UITabBarController {
    
    
    func composeButtonClick(){
        
        print("__FUNCTION__")
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewController()
//        
//       123 
//        let newTabBar=AMTabbar()
//        
//        setValue(newTabBar, forKey: "tabBar")
//        
//        newTabBar.compostButton.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
//        
        
        //let width = tabBar.bounds.size.width/5
        //let frame = CGRect(x: 2*width, y: 0, width: width, height: tabBar.bounds.height)
       // compostButton.frame=frame
        
        
      
    }
    
    //上面的时候占位会被item给挡住
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        
        let width = tabBar.bounds.size.width/5
        let frame = CGRect(x: 2*width, y: 0, width: width, height: tabBar.bounds.height)
        compostButton.frame=frame
       
        
    }
    
    
    
    private func addChildViewController() {
        //
        tabBar.tintColor = UIColor.orangeColor()
        
        
        let homeTableVC=AMHomeTableViewController()
        addChildViewController(homeTableVC, title: "首页", imageName: "tabbar_home")
        
        let messageTableVC=AMmessageTableViewController()
        addChildViewController(messageTableVC, title: "消息", imageName: "tabbar_message_center")
        
        //占位
        addChildViewController(UIViewController())
        
        
  
        let discoverTableVC=AMdiscoverTableViewController()
        addChildViewController(discoverTableVC, title: "发现", imageName: "tabbar_discover")
      
        let profileTableVC=AMProfileTableViewController()
        addChildViewController(profileTableVC, title: "我", imageName: "tabbar_profile")
        
    }
    //抽取
    
    private func addChildViewController(controller:UIViewController,title:String,imageName:String){
        controller.title=title
        controller.tabBarItem.image=UIImage(named: imageName)
        let profileNav=UINavigationController(rootViewController: controller)
        addChildViewController(profileNav)
        
        
        // 拼接高亮图片名称
//        let highlightedImageName = imageName + "_highlighted"
//        // 让选中的图片使用原来的颜色,不要系统帮我们渲染
//        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        
//        // 设置高亮图片
//        controller.tabBarItem.selectedImage = highlightedImage
//        
       //  设置标题颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Highlighted)
//        
//        
        
       
}
    
    //撰写按钮懒加载
    
   
  
    lazy var compostButton: UIButton = {
        
        let button = UIButton()
        
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮图片
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        
      //  self.addSubview(button)
      //控制器里没有
        self.tabBar.addSubview(button)
        
        return button
        
    }()
   
    

}